package com.cloud.business.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.business.dao.sys.SysSellerUserMapper;
import com.cloud.business.entity.sys.SysSellerUser;
import com.cloud.business.entity.sys.SysUser;
import com.cloud.business.model.commonvm.DataDropDown;
import com.cloud.business.model.commonvm.DeletePost;
import com.cloud.business.model.commonvm.ResultMsg;
import com.cloud.business.model.sys.sysselleruservm.SysSellerUserModel;
import com.cloud.business.model.sys.sysselleruservm.SysSellerUserPost;
import com.cloud.business.model.sys.sysselleruservm.SysSellerUserQueryModel;
import com.cloud.business.service.sys.ISysSellerUserService;
import com.cloud.core.utils.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 系统用户业务实现层
 * Copyright: Copyright (C) 2020 DLANGEL, Inc. All rights reserved.
 * Company: 大连安琪科技有限公司
 *
 * @author Rex
 * @since 2021-05-16 14:22:52
 */
@Service
public class SysSellerUserServiceImpl implements ISysSellerUserService {

    @Resource
    private SysSellerUserMapper sysSellerUserMapper;

    //region 获取分页列表

    /**
     * 分页列表
     *
     * @param sysSellerUserQueryModel
     * @param page
     * @param msg
     * @throws Exception
     */
    @Override
    public void getSysSellerUserListByPage(SysSellerUserQueryModel sysSellerUserQueryModel, IPage<SysSellerUserModel> page, ResultMsg msg) throws Exception {

        if (page.getSize() > 0) {
            long startNum = (sysSellerUserQueryModel.getPageNum() - 1) * sysSellerUserQueryModel.getRecordNum();
            sysSellerUserQueryModel.setStartNum(startNum);
        }

        // 当排序字段是姓名，则按照汉字首字母排序
        if ("true_name".equals(sysSellerUserQueryModel.getSortField())) {
            sysSellerUserQueryModel.setSortField("fristAlphabet(true_name)");
        }

        List<SysSellerUserModel> list = sysSellerUserMapper.getListByPage(sysSellerUserQueryModel);
        Long count = sysSellerUserMapper.getListCount(sysSellerUserQueryModel);

        page.setRecords(list);
        page.setTotal(count);
        msg.setMsg("获取成功");
    }
    //endregion

    //region 创建

    /**
     * 创建
     *
     * @param post 入参
     * @return {@link String}
     * @author Rex
     */
    @Override
    public String create(SysSellerUserPost post) {
        String msg = "创建成功";
        Date now = new Date();
        SysSellerUser sysSellerUser = new SysSellerUser();
        BeanUtils.copyProperties(post, sysSellerUser);
        sysSellerUser.setCreateTime(now);
        sysSellerUser.setModificationTime(now);
        int count = sysSellerUserMapper.insert(sysSellerUser);
        return msg;
    }
    //endregion

    //region 更新

    /**
     * 更新
     *
     * @param post 入参
     * @return {@link String}
     * @author Rex
     */
    @Override
    public String update(SysSellerUserPost post) {
        String msg = "更新成功";
        SysSellerUser sysSellerUser = sysSellerUserMapper.selectById(post.getObjectId());
        BeanUtils.copyProperties(post, sysSellerUser);
        Date now = new Date();
        sysSellerUser.setModificationTime(now);
        int count = sysSellerUserMapper.updateById(sysSellerUser);
        return msg;
    }
    //endregion

    //region 获取单条

    /**
     * 获取单条
     *
     * @param objectId 主键
     * @return {@link SysSellerUser}
     * @author Rex
     */
    @Override
    public SysSellerUser getById(Long objectId) {
        SysSellerUser sysSellerUser = sysSellerUserMapper.selectById(objectId);
        return sysSellerUser;
    }
    //endregion

    //region 批量删除

    /**
     * 批量删除
     *
     * @param deletePost 主键集合
     * @return {@link String}
     * @author Rex
     */
    @Override
    public String deleteByList(DeletePost deletePost) {
        String msg = "删除成功";
        int count = sysSellerUserMapper.deleteBatchIds(deletePost.getKeyValues());
        if (count <= 0) {
            throw new CustomException("删除失败");
        }
        return msg;
    }

    /**
     * 批量创建
     *
     * @param sysSellerUsers 入参
     * @return {@link String}
     * @author Rex
     */
    @Override
    public String createBatch(List<SysSellerUser> sysSellerUsers) {
        String msg = "批量创建成功";
        int loopCnt = sysSellerUsers.size() / 5000;
        System.out.println("共有" + sysSellerUsers.size() + " 条数据，每组5000共分成 " + loopCnt + " 组");
        int startIndex = 0;
        for (int i = 0; i < loopCnt; i++) {
            int toIndex = 5000 * i;
            List<SysSellerUser> tempList = sysSellerUsers.subList(startIndex, toIndex);
            System.out.println("第" + (i + 1) + " 次导入5000条数据");
            sysSellerUserMapper.insertBatch(tempList);
            startIndex = toIndex + 1;
        }
        return msg;
    }

    @Override
    public List<SysSellerUser> getSysSellerUserListForExport(SysSellerUserQueryModel sysSellerUserQueryModel) throws Exception {
        return sysSellerUserMapper.getListForExport(sysSellerUserQueryModel);
    }
    //endregion

}
